package com.bw.mallshop.mvvm.viewmodel;

import android.text.TextUtils;

import androidx.databinding.ObservableField;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.entity.ResetPWDEntity;
import com.bw.mallshop.mvvm.model.ResetModel;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ResetViewModel extends BaseViewModel<ResetModel> {

    public String phone;
    public ObservableField<String>pwd = new ObservableField<>();
    public ObservableField<String>confirmpwd = new ObservableField<>();

    public void confirm(){
        if (TextUtils.isEmpty(pwd.get())){
            showErro("密码为空");
            return;
        }
        if (TextUtils.isEmpty(confirmpwd.get())){
            showErro("确认密码为空");
            return;
        }
        if (!pwd.get().equals(confirmpwd.get())){
            showErro("密码不同");
            return;
        }
        Map<String,Object>map=new HashMap<>();
        map.put("mobile",phone);
        map.put("pwd",pwd.get());
        m.requestReset(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void finish(){
        finishPage();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void result(BaseEntity entity) {
         if (entity instanceof ResetPWDEntity){
             ResetPWDEntity resetPWDEntity = (ResetPWDEntity) entity;
             showErro(resetPWDEntity.message);
             if (resetPWDEntity.message.equals("密码修改成功")){
                 finishPage();
             }
         }
    }
}
